package cn.yunhe.controller;

import cn.yunhe.entity.Exam;
import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.User;
import cn.yunhe.service.impl.ExamServiceImpl;
import cn.yunhe.util.DateUtil;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ExamController {

    @Resource
    private ExamServiceImpl examService;

    @RequiresPermissions("/view/exam-list.html")
    @RequestMapping("/toExam.do")
    public void toExam(HttpServletResponse response) throws IOException {
        response.sendRedirect("/view/exam-list.html");
    }

    @RequestMapping("getExamByPage")
    public Map getExamByPage(int pageSize, int currPage, Exam exam, String exampaper, String username, String starttime, String endtime) {
        if (exampaper != null && !"".equals(exampaper)) {
            ExamPaper examPaper = new ExamPaper();
            examPaper.setName(exampaper);
            exam.setExamPaper(examPaper);
        }
        if (username != null && !"".equals(username)) {
            User user = new User();
            user.setUserName(username);
            exam.setUser(user);
        }
        try{
        if(starttime != null && !"".equals(starttime)){
            exam.setStartTime(DateUtil.String2DateTime(starttime));
        }
        if(endtime != null && !"".equals(endtime)){
            exam.setEndTime(DateUtil.String2DateTime(endtime));
        }}catch (Exception e){}
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("currPage", currPage);
        map.put("exam", exam);
        Page page = examService.getExamByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequiresPermissions("/updExamAudit")
    @RequestMapping("updExamAudit")
    public int updExamAudit(Exam exam){
        return examService.updExamAudit(exam);
    }

    @RequestMapping("updExamInfo")
    public int updExamInfo(String id, String examName, int examType, String examIntro, String examPaperId, String startTime, String endTime){
        Exam exam = new Exam();
        exam.setId(Integer.valueOf(id));
        exam.setExamName(examName);
        exam.setExamType((byte) examType);
        exam.setExamIntro(examIntro);
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(Integer.valueOf(examPaperId));
        exam.setExamPaper(examPaper);
        try {
            exam.setStartTime(DateUtil.String2DateTime(startTime));
            exam.setEndTime(DateUtil.String2DateTime(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return examService.updExamInfo(exam);
    }

    @RequiresPermissions("/delExam")
    @RequestMapping("delExam")
    public int delExam(int id){
        return examService.delExam(id);
    }

    @RequiresPermissions("/getExamById")
    @RequestMapping("getExamById")
    public ModelAndView getExamById(int id) {
        ModelAndView mv = new ModelAndView("editExamInfo");
        Exam exam = examService.getExamById(id);
        String start = DateUtil.DateTime2String(exam.getStartTime());
        String end = DateUtil.DateTime2String(exam.getEndTime());
        if(exam.getExamIntro() == null){
            exam.setExamIntro(" ");
        }
        Map<String, Object>map = new HashMap<>();
        map.put("exam", exam);
        map.put("start",start);
        map.put("end",end);
        mv.addObject("map", map);
        return mv;
    }

    @RequestMapping("/ExamAddExam")
    public int addExam(HttpSession session, String examName, int examType, String examIntro, String examPaperId, String startTime, String endTime){
        //封装数据
        Exam exam = new Exam();
        exam.setExamName(examName);
        exam.setExamType((byte)examType);
        if(examType == 2){
            exam.setApproved((byte)1);
        }else {
            exam.setApproved((byte)0);
        }
        exam.setExamIntro(examIntro);
        //封装时间
        try {
            //System.out.println(startTime);
            Date sTime = DateUtil.String2DateTime(startTime);
            Date eTime = DateUtil.String2DateTime(endTime);
            Date cTime = new Date();
            exam.setCreateTime(cTime);
            exam.setStartTime(sTime);
            exam.setEndTime(eTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //封装用户
        User user= (User) session.getAttribute("Curr_User");
        exam.setUser(user);
        //封装试卷
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(Integer.valueOf(examPaperId));
        exam.setExamPaper(examPaper);

        return examService.addExam(exam);
    }


    @RequestMapping("/getAllApplication")
    public Map queryFieldByPage(int pageSize, int pageNum) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        Page page = examService.getExamApplication(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    /**
     * 考试历史列表
     * @param pageSize 页大小
     * @param pageNum 访问页
     * @param examName 考试名字
     * @param examId 考试id
     * @param startTime 考试时间
     * @return
     */
    @RequestMapping("/getUserExamInfo")
    public Map getUserExamInfo(int pageSize,int pageNum,String examName,Integer examId,String startTime){
        Date sTime = null;
        if (startTime !=null && !"".equals(startTime)){
            try {
                sTime = DateUtil.String2DateTime(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Map map = new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum", pageNum);
        map.put("examName",examName);
        map.put("examId",examId);
        map.put("startTime",sTime);
        Page page = examService.getUserExamInfo(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/view/toExampast_userList")
    public ModelAndView toExampast_userList(HttpServletRequest request,Integer examId){
        request.setAttribute("examId",examId);
        return new ModelAndView("exampast_userList");
    }


    @RequestMapping("/updApplication")
    public int updApplication(int ueid){
        return examService.updExamApplication(ueid);
    }

    @RequestMapping("/delApplication")
    public int delApplication(int ueid){
        return examService.delApplication(ueid);
    }


    public ExamServiceImpl getExamService() {
        return examService;
    }

    public void setExamService(ExamServiceImpl examService) {
        this.examService = examService;
    }
}
